/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import businessObjects.Esquema;
import daos.exceptions.IllegalOrphanException;
import daos.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import businessObjects.Pauta;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author williamprado
 */
public class EsquemaDAO {

    public EsquemaDAO() {
        //emf = Persistence.createEntityManagerFactory("Adz-ejbPU");
    }
    //private EntityManagerFactory emf = null;

    /*public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }*/

    /*public void create(Esquema esquema) {
        if (esquema.getPautaCollection() == null) {
            esquema.setPautaCollection(new ArrayList<Pauta>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Pauta> attachedPautaCollection = new ArrayList<Pauta>();
            for (Pauta pautaCollectionPautaToAttach : esquema.getPautaCollection()) {
                pautaCollectionPautaToAttach = em.getReference(pautaCollectionPautaToAttach.getClass(), pautaCollectionPautaToAttach.getIdPauta());
                attachedPautaCollection.add(pautaCollectionPautaToAttach);
            }
            esquema.setPautaCollection(attachedPautaCollection);
            em.persist(esquema);
            for (Pauta pautaCollectionPauta : esquema.getPautaCollection()) {
                Esquema oldIdEsquemaOfPautaCollectionPauta = pautaCollectionPauta.getIdEsquema();
                pautaCollectionPauta.setIdEsquema(esquema);
                pautaCollectionPauta = em.merge(pautaCollectionPauta);
                if (oldIdEsquemaOfPautaCollectionPauta != null) {
                    oldIdEsquemaOfPautaCollectionPauta.getPautaCollection().remove(pautaCollectionPauta);
                    oldIdEsquemaOfPautaCollectionPauta = em.merge(oldIdEsquemaOfPautaCollectionPauta);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void create(Esquema esquema, EntityManager em) throws Exception {
        if (esquema.getPautaCollection() == null) {
            esquema.setPautaCollection(new ArrayList<Pauta>());
        }
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Collection<Pauta> attachedPautaCollection = new ArrayList<Pauta>();
            for (Pauta pautaCollectionPautaToAttach : esquema.getPautaCollection()) {
                pautaCollectionPautaToAttach = em.getReference(pautaCollectionPautaToAttach.getClass(), pautaCollectionPautaToAttach.getIdPauta());
                attachedPautaCollection.add(pautaCollectionPautaToAttach);
            }
            esquema.setPautaCollection(attachedPautaCollection);
            em.persist(esquema);
            for (Pauta pautaCollectionPauta : esquema.getPautaCollection()) {
                Esquema oldIdEsquemaOfPautaCollectionPauta = pautaCollectionPauta.getIdEsquema();
                pautaCollectionPauta.setIdEsquema(esquema);
                pautaCollectionPauta = em.merge(pautaCollectionPauta);
                if (oldIdEsquemaOfPautaCollectionPauta != null) {
                    oldIdEsquemaOfPautaCollectionPauta.getPautaCollection().remove(pautaCollectionPauta);
                    oldIdEsquemaOfPautaCollectionPauta = em.merge(oldIdEsquemaOfPautaCollectionPauta);
                }
            }
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public void edit(Esquema esquema) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Esquema persistentEsquema = em.find(Esquema.class, esquema.getIdEsquema());
            Collection<Pauta> pautaCollectionOld = persistentEsquema.getPautaCollection();
            Collection<Pauta> pautaCollectionNew = esquema.getPautaCollection();
            List<String> illegalOrphanMessages = null;
            for (Pauta pautaCollectionOldPauta : pautaCollectionOld) {
                if (!pautaCollectionNew.contains(pautaCollectionOldPauta)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Pauta " + pautaCollectionOldPauta + " since its idEsquema field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Pauta> attachedPautaCollectionNew = new ArrayList<Pauta>();
            for (Pauta pautaCollectionNewPautaToAttach : pautaCollectionNew) {
                pautaCollectionNewPautaToAttach = em.getReference(pautaCollectionNewPautaToAttach.getClass(), pautaCollectionNewPautaToAttach.getIdPauta());
                attachedPautaCollectionNew.add(pautaCollectionNewPautaToAttach);
            }
            pautaCollectionNew = attachedPautaCollectionNew;
            esquema.setPautaCollection(pautaCollectionNew);
            esquema = em.merge(esquema);
            for (Pauta pautaCollectionNewPauta : pautaCollectionNew) {
                if (!pautaCollectionOld.contains(pautaCollectionNewPauta)) {
                    Esquema oldIdEsquemaOfPautaCollectionNewPauta = pautaCollectionNewPauta.getIdEsquema();
                    pautaCollectionNewPauta.setIdEsquema(esquema);
                    pautaCollectionNewPauta = em.merge(pautaCollectionNewPauta);
                    if (oldIdEsquemaOfPautaCollectionNewPauta != null && !oldIdEsquemaOfPautaCollectionNewPauta.equals(esquema)) {
                        oldIdEsquemaOfPautaCollectionNewPauta.getPautaCollection().remove(pautaCollectionNewPauta);
                        oldIdEsquemaOfPautaCollectionNewPauta = em.merge(oldIdEsquemaOfPautaCollectionNewPauta);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = esquema.getIdEsquema();
                if (findEsquema(id) == null) {
                    throw new NonexistentEntityException("The esquema with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void edit(Esquema esquema, EntityManager em) throws IllegalOrphanException, NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Esquema persistentEsquema = em.find(Esquema.class, esquema.getIdEsquema());
            Collection<Pauta> pautaCollectionOld = persistentEsquema.getPautaCollection();
            Collection<Pauta> pautaCollectionNew = esquema.getPautaCollection();
            List<String> illegalOrphanMessages = null;
            for (Pauta pautaCollectionOldPauta : pautaCollectionOld) {
                if (!pautaCollectionNew.contains(pautaCollectionOldPauta)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Pauta " + pautaCollectionOldPauta + " since its idEsquema field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Pauta> attachedPautaCollectionNew = new ArrayList<Pauta>();
            for (Pauta pautaCollectionNewPautaToAttach : pautaCollectionNew) {
                pautaCollectionNewPautaToAttach = em.getReference(pautaCollectionNewPautaToAttach.getClass(), pautaCollectionNewPautaToAttach.getIdPauta());
                attachedPautaCollectionNew.add(pautaCollectionNewPautaToAttach);
            }
            pautaCollectionNew = attachedPautaCollectionNew;
            esquema.setPautaCollection(pautaCollectionNew);
            esquema = em.merge(esquema);
            for (Pauta pautaCollectionNewPauta : pautaCollectionNew) {
                if (!pautaCollectionOld.contains(pautaCollectionNewPauta)) {
                    Esquema oldIdEsquemaOfPautaCollectionNewPauta = pautaCollectionNewPauta.getIdEsquema();
                    pautaCollectionNewPauta.setIdEsquema(esquema);
                    pautaCollectionNewPauta = em.merge(pautaCollectionNewPauta);
                    if (oldIdEsquemaOfPautaCollectionNewPauta != null && !oldIdEsquemaOfPautaCollectionNewPauta.equals(esquema)) {
                        oldIdEsquemaOfPautaCollectionNewPauta.getPautaCollection().remove(pautaCollectionNewPauta);
                        oldIdEsquemaOfPautaCollectionNewPauta = em.merge(oldIdEsquemaOfPautaCollectionNewPauta);
                    }
                }
            }
            //em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = esquema.getIdEsquema();
                if (findEsquema(id, em) == null) {
                    throw new NonexistentEntityException("The esquema with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    /*public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Esquema esquema;
            try {
                esquema = em.getReference(Esquema.class, id);
                esquema.getIdEsquema();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The esquema with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Pauta> pautaCollectionOrphanCheck = esquema.getPautaCollection();
            for (Pauta pautaCollectionOrphanCheckPauta : pautaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Esquema (" + esquema + ") cannot be destroyed since the Pauta " + pautaCollectionOrphanCheckPauta + " in its pautaCollection field has a non-nullable idEsquema field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(esquema);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void destroy(Integer id, EntityManager em) throws IllegalOrphanException, NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Esquema esquema;
            try {
                esquema = em.getReference(Esquema.class, id);
                esquema.getIdEsquema();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The esquema with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Pauta> pautaCollectionOrphanCheck = esquema.getPautaCollection();
            for (Pauta pautaCollectionOrphanCheckPauta : pautaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Esquema (" + esquema + ") cannot be destroyed since the Pauta " + pautaCollectionOrphanCheckPauta + " in its pautaCollection field has a non-nullable idEsquema field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(esquema);
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public List<Esquema> findEsquemaEntities() {
        return findEsquemaEntities(true, -1, -1);
    }*/

    public List<Esquema> findEsquemaEntities(EntityManager em) throws Exception {
        return findEsquemaEntities(true, -1, -1, em);
    }

    /*public List<Esquema> findEsquemaEntities(int maxResults, int firstResult) {
        return findEsquemaEntities(false, maxResults, firstResult);
    }*/

    public List<Esquema> findEsquemaEntities(int maxResults, int firstResult, EntityManager em) throws Exception {
        return findEsquemaEntities(false, maxResults, firstResult, em);
    }

    /*private List<Esquema> findEsquemaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Esquema as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }*/

    private List<Esquema> findEsquemaEntities(boolean all, int maxResults, int firstResult, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Esquema as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public Esquema findEsquema(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Esquema.class, id);
        } finally {
            em.close();
        }
    }*/

    public Esquema findEsquema(Integer id, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            return em.find(Esquema.class, id);
        } catch(Exception e) {
            throw e;
        }
    }

    /*public int getEsquemaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Esquema as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }*/

    public int getEsquemaCount(EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Esquema as o");
            return ((Long) q.getSingleResult()).intValue();
        } catch(Exception e) {
           throw e;
        }
    }

}
